package com.example.demo4.controller;

import com.example.demo4.entity.Book;
import com.example.demo4.entity.RespBean;
import com.example.demo4.service.BookService;
import com.example.demo4.service.DateService;
import com.example.demo4.service.SchemaNumberService;
import com.example.demo4.service.SchemaService;
import com.example.demo4.util.getDate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.rmi.CORBA.Util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: lin
 * @description:
 * 排班开始日期beginDate
 *排班结束日期finishDate
 * 早班人数 MorningNum
 * 中班人数NoonNum
 * 晚班人数NightNum
 * 工作日是否排班WorkDaySchema
 * @time: 2021/7/30 16:20
 */
@RestController
@Transactional
//@CrossOrigin(origins = {"http://localhost:8080","null"})
@RequestMapping(value = "/schema")
public class SchemaController {
    @Autowired
    private DateService dateService;
    @Autowired
    private SchemaService schemaService;
    @Autowired
    private SchemaNumberService schemaNumberService;
    @RequestMapping(value = "/schemahelp", method = RequestMethod.POST)
    public RespBean schema( String beginDate,String finishDate,  int MorningNum, int NoonNum, int NightNum,  boolean restDaySchema,  boolean ContinueSchema) throws ParseException {
        System.out.println("排班开始日期:" + beginDate
                + "排班结束日期:" + finishDate
                + "早班人数:" + MorningNum
                + "中班人数:" + NoonNum
                + "晚班人数:" + NightNum
                + "工作日是否排班:" + restDaySchema
                + "该日期已有排班是否继续排班："+ ContinueSchema);
        //已有排班的日期
        ArrayList<String> AlreadySchemaList = new ArrayList<>();
        //获取时间段日期
        List<String> daysStr = getDate.findDaysStr(beginDate, finishDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //休息日也排班
        if(restDaySchema == true){
            for (String s : daysStr) {
                Date date = sdf.parse(s);
                if (dateService.dateIsSchema(date)) {
                    AlreadySchemaList.add(s);
                }else {
                    //单日排班
                    schemaService.schmaBydate(date,MorningNum,NoonNum,NightNum);
                }
            }
            if(AlreadySchemaList.isEmpty()) {
            return RespBean.ok("排班成功");
            }else {
                return RespBean.ok("日期"+ StringUtils.strip(AlreadySchemaList.toString(),"[]")+"已有排班信息",AlreadySchemaList);
            }
        }else {       //休息日不排班
            for (String s : daysStr) {
                Date date = sdf.parse(s);
                //如果为工作日就排班
                if (dateService.getStateByDate(date)==0){
                    //如果当日有排班
                    if (dateService.dateIsSchema(date)) {
                        AlreadySchemaList.add(s);
                    }else {
                        //当日排班
                        schemaService.schmaBydate(date,MorningNum,NoonNum,NightNum);
                    }
                }
            }
            if(AlreadySchemaList.isEmpty()) {
                return RespBean.ok("排班成功");
            }else {
                return RespBean.ok("日期"+ StringUtils.strip(AlreadySchemaList.toString(),"[]")+"已有排班信息",AlreadySchemaList);
            }
        }

        //boolean dateIsSchema = dateService.dateIsSchema(beginDate,finishDate);


    }
    //弹窗选择为是
    @RequestMapping(value = "/pop-upIsY",method = RequestMethod.POST)
    public RespBean popUpIsY(@RequestParam(value = "alreadyList[]") List<String> alreadyList,int MorningNum, int NoonNum, int NightNum) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取已排班日期
        for (String s : alreadyList) {
            //对日期进行重新排班
            Date date = sdf.parse(s);
            schemaService.schmaBydate(date,MorningNum,NoonNum,NightNum);
        }
        return RespBean.ok("重新排班成功");
    }
    //弹窗选择为否
    public RespBean popUpIsN(){

        return RespBean.ok("弹窗选择为否，不用重新排班");
    }
    //显示早中晚排班人数
    public RespBean showSchema() {
        Map<String, Integer> map = new HashMap<>();
        //显示班次人数
        List showList = schemaNumberService.showList();
        return RespBean.ok("显示早中晚排班人数",showList);
    }
    //显示员工排班信息
    public RespBean showEmpSchema(String dates) throws ParseException {
        //显示员工排班信息
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dates);
        List showList = schemaService.showList(date);
        return RespBean.ok("显示员工排班信息",showList);
    }
    //添加排班
    public RespBean AddSchema(String Dates,String name,int id,int classes) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(Dates);
        schemaService.save(date,name,id,classes);
        return RespBean.ok("添加排班成功");
    }
    //删除排班
    public RespBean delectSchema(String Dates,String name,int id,int classes) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(Dates);
        int flag = schemaService.delectSchema(date,name,id,classes);
        return RespBean.ok("删除排班成功");
    }
    //修改排班(传入员工号和班次和日期）1:早 2：中 3：晚
    public RespBean updateSchema(String Dates,int id,int classes) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(Dates);
        int flag = schemaService.updateSchema(date,id,classes);
        return RespBean.ok("修改排班成功");
    }
}
